package com.zl.smartmall.library.po;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.util.CrashHandler;

/**
 * 订单里的商品
 * 
 * @author 陈希然
 * 
 */
public class OrderCommoditiesInfo {
	private int activitiesId;//活动id
	private int buyNum;// 购买数量
	private int productId;// 商品id
	private int shopId;// 商铺id
	private double price;// 价格
	private String productTitle;// 商品标题
	private String productName;// 商品名字
	private String productPicture;// 商品图片
	private String shopName;
	
	public int getActivitiesId() {
		return activitiesId;
	}

	public void setActivitiesId(int activitiesId) {
		this.activitiesId = activitiesId;
	}

	public int getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(int buyNum) {
		this.buyNum = buyNum;
	}

	public int getProductId() {
		return productId;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public int getShopId() {
		return shopId;
	}

	public void setShopId(int shopId) {
		this.shopId = shopId;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getProductTitle() {
		return productTitle;
	}

	public void setProductTitle(String productTitle) {
		this.productTitle = productTitle;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductPicture() {
		return productPicture;
	}

	public void setProductPicture(String productPicture) {
		this.productPicture = productPicture;
	}
	
	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public JSONObject encodeJson() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("product_id", productId);
			jsonObject.put("shop_id", shopId);
			jsonObject.put("num", buyNum);
			jsonObject.put("price", price);
			jsonObject.put("act_id", activitiesId);
			
		} catch (Exception e) {
			CrashHandler.e(e);
		}

		return jsonObject;
	}
	
	public static List<OrderCommoditiesInfo> parse(JSONArray jsonArray) throws JSONException {
		List<OrderCommoditiesInfo> orderCommoditiesInfos = new ArrayList<OrderCommoditiesInfo>();
		for(int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			OrderCommoditiesInfo orderCommoditiesInfo = new OrderCommoditiesInfo();
			orderCommoditiesInfo.buyNum = jsonObject.getInt("product_num");
			orderCommoditiesInfo.price = jsonObject.getDouble("sale_price");
			orderCommoditiesInfo.productId = jsonObject.getInt("product_id");
			orderCommoditiesInfo.productName = jsonObject.getString("product_name");
			orderCommoditiesInfo.productPicture = jsonObject.getString("title_pic");
			orderCommoditiesInfo.productTitle = jsonObject.getString("product_title");
			orderCommoditiesInfo.shopId = jsonObject.getInt("shop_id");
			orderCommoditiesInfo.activitiesId = jsonObject.getInt("act_id");
			if(jsonObject.has("shop_name"))
				orderCommoditiesInfo.shopName = jsonObject.getString("shop_name");
			orderCommoditiesInfos.add(orderCommoditiesInfo);
		}
		return orderCommoditiesInfos;
	}
}
